import java.io.IOException;
import java.io.InputStream;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.m2g.SVGAnimator;
import javax.microedition.m2g.SVGEventListener;
import javax.microedition.m2g.SVGImage;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class ViewSVGImageAnimation extends MIDlet implements CommandListener{
	//
	private Display display;
	private SVGImage image;
	private SVGAnimator animator;
	private Command stopSVGAnimation;
	private Command playSVGAnimation;
	private Command pauseSVGAnimation;
	private Canvas canvas;

	//
	public ViewSVGImageAnimation() {
		// TODO Auto-generated constructor stub
		this.stopSVGAnimation = new Command("Stop Animation",Command.SCREEN,1);
		this.playSVGAnimation = new Command("Play Animation",Command.SCREEN,1);
		this.pauseSVGAnimation = new Command("Pause Animation",Command.SCREEN,1);
		display = Display.getDisplay(this);
		InputStream inputStream = getClass()
				.getResourceAsStream("/weather.svg");
		try {
			this.image = (SVGImage) SVGImage.createImage(inputStream, null);
			this.animator = SVGAnimator.createAnimator(this.image);
			this.canvas = (Canvas) this.animator.getTargetComponent();
			//Thiết lập các xự kiện play,stop,pause hoạt cảnh của svg này
			this.canvas.addCommand(this.stopSVGAnimation);
			this.canvas.addCommand(this.playSVGAnimation);
			this.canvas.addCommand(this.pauseSVGAnimation);
			this.canvas.setCommandListener(this);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		display.setCurrent(this.canvas);
	}

	public void commandAction(Command arg0, Displayable arg1) {
		// TODO Auto-generated method stub
		if(arg0 == this.stopSVGAnimation){
			this.animator.stop();
		}else if(arg0 == this.playSVGAnimation){
			this.animator.play();
		}else if(arg0 == this.pauseSVGAnimation){
			this.animator.pause();
		}
	}

}
//
//